package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("users")
@Api(value = "用户资源",description = "用户资源控制器")
public class UserInfoController {

	@Autowired
	private IUserInfoService userInfoService;
	@Autowired
	private IUserInfoRedisService userInfoRedisService;
	@Autowired
	private IStrategyService strategyService;
	@Autowired
	private ITravelService travelService;

	@GetMapping("/list")
	public Object list() {
		return userInfoService.list();
	}

	@GetMapping("/checkPhone")
	public Object checkPhone(String phone) {
		return !userInfoService.checkPhone(phone);
	}

	@ApiResponses({
			@ApiResponse(code=200,message="用户注册成功")
	})
	@GetMapping("/sendVerifyCode")
	public Object sendVerifyCode(String phone) {
		userInfoService.sendVerifyCode(phone);
		return JsonResult.success();
	}

	@ApiImplicitParams({
			@ApiImplicitParam(value = "手机号",name = "phone",dataType = "String",required = true),
			@ApiImplicitParam(value = "昵称",name = "nickname",dataType = "String",required = true),
			@ApiImplicitParam(value = "密码",name = "password",dataType = "String",required = true),
			@ApiImplicitParam(value = "确认密码",name = "rpassword",dataType = "String",required = true),
			@ApiImplicitParam(value = "验证码",name = "verifyCode",dataType = "String",required = true)
	})
	@PostMapping("/regist")
	public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
		userInfoService.regist(phone,nickname,password,rpassword,verifyCode);
		return JsonResult.success();
	}

	@PostMapping("/login")
	public Object login(String username,String password) {
		UserInfo user = userInfoService.findByPhoneByPassword(username,password);

		String token = UUID.randomUUID().toString().replaceAll("-", "");
		userInfoRedisService.setToken(token,user);

		Map<String,Object> map = new HashMap<>();
		map.put("token", token);
		map.put("user", user);
		return JsonResult.success(map);
	}
	@RequireLogin
	@GetMapping("/currentUser")
	public Object currentUser(HttpServletRequest request) {
		// 从请求头拿到token
		String token = request.getHeader("token");
		// 查询redis中对应key(token)的value(user)
		UserInfo user = userInfoRedisService.getUserByToken(token);
		return JsonResult.success(user);
	}
	// 查看攻略收藏列表
	@GetMapping("/strategies/favor")
	public Object strategiesFavor(String userId) {
		List<String> sidList = strategyService.getStrategiesFavor(userId);
		return JsonResult.success(sidList);
	}
	// 查看游记收藏列表
	@GetMapping("/travels/favor")
	public Object travelsFavor(String userId) {
		List<String> tidList = travelService.getTravelsFavor(userId);
		return JsonResult.success(tidList);
	}
}